//
//  RadarPictureView.m
//  carcareIOS
//
//  Created by wr on 16/2/23.
//  Copyright © 2016年 baozun. All rights reserved.
//

#import "RadarPictureView.h"

typedef NS_ENUM(NSInteger , VIEW_TYPE) {
    //四个端点的view Tag
    VIEW_TYPE_LEFT = 10,
    VIEW_TYPE_TOP ,
    VIEW_TYPE_RIGHT ,
    VIEW_TYPE_BOTTOM,
    
    //四个端点Label
    LABEL_TYPE_LEFT = 20,
    LABEL_TYPE_TOP ,
    LABEL_TYPE_RIGHT,
    LABEL_TYPE_BOTTOM,
    

};

@interface RadarPictureView()

@property (nonatomic , copy) NSArray * dataArray;

//中心分数的label
@property (nonatomic , strong) UILabel * scoreLabel;

//急加速次数
@property (nonatomic , strong) UILabel * topTimesLabel;

//急减速次数
@property (nonatomic , strong) UILabel * rightTimesLabel;

@end
@implementation RadarPictureView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
      
        self.scoreLabel = [Factory labelWithFrame:CGRectZero font:FONT_DEFAULT_Bold(40 * __MAIN_RATIO_375) text:@"" textColor:[UIColor whiteColor] onView:self textAlignment:NSTextAlignmentLeft];
        for (int index = 0; index < 4; index ++) {
            UIView * cornerView = [[[UIView viewWithFrame:CGRectZero] resetBackgroundColor:[UIColor clearColor]] setupOnView:self];
            cornerView.size = CGSizeMake(4, 4);
            cornerView.layer.cornerRadius = cornerView.size.width / 2;
            cornerView.layer.masksToBounds = YES;
            cornerView.tag = (index == 0 ? VIEW_TYPE_LEFT : (index == 1 ? VIEW_TYPE_TOP : (index == 2 ? VIEW_TYPE_RIGHT : (index == 3 ? VIEW_TYPE_BOTTOM : 0))));
            cornerView.backgroundColor = (index == 0 ? COLOR_HEXSTRING(@"#31AADA") : (index == 1 ? COLOR_HEXSTRING(@"#C870F1") : (index == 2 ? COLOR_HEXSTRING(@"F16A9D") : (index == 3 ? COLOR_HEXSTRING(@"#43E6AF") : [UIColor clearColor]))));
            
            UILabel * label = [Factory labelWithFrame:CGRectZero font:FONT_DEFAULT_Light(15 * __MAIN_RATIO_375) text:@"" textColor:[Theme wordColor] onView:self textAlignment:NSTextAlignmentCenter];
            label.backgroundColor = [UIColor clearColor];
            label.tag = (index == 0 ? LABEL_TYPE_LEFT : (index == 1 ? LABEL_TYPE_TOP : (index == 2 ? LABEL_TYPE_RIGHT : (index == 3 ? LABEL_TYPE_BOTTOM : 0))));;
        }
       self.topTimesLabel = [Factory labelWithFrame:CGRectZero font:FONT_DEFAULT_Light(15 * __MAIN_RATIO_375) text:@"" textColor:[Theme wordColor] onView:self textAlignment:NSTextAlignmentCenter];
        self.topTimesLabel.layer.cornerRadius = 2;
        self.topTimesLabel.layer.masksToBounds = YES;
        self.topTimesLabel.backgroundColor = [Theme backGroundColor];
        self.rightTimesLabel = [Factory labelWithFrame:CGRectZero font:FONT_DEFAULT_Light(15 * __MAIN_RATIO_375) text:@"" textColor:[Theme wordColor] onView:self textAlignment:NSTextAlignmentCenter];
        self.rightTimesLabel.layer.cornerRadius = 2;
        self.rightTimesLabel.layer.masksToBounds = YES;
        self.rightTimesLabel.backgroundColor = [Theme backGroundColor];
        
    }
    return self;
}
- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    CGFloat widthAndHeight = 271 * __MAIN_RATIO_375;
    CGPoint centerPoint = CGPointMake(self.width / 2, self.height / 2);
    
    CGFloat triangleWidth = widthAndHeight / 2;//每个小三角形横行宽度
    CGFloat triangleHeight = triangleWidth;//每个小三角形垂直高度
    CGFloat leftSmallX = (self.width - widthAndHeight) / 2;//图形距离左侧的距离
    CGFloat topSmallY = (self.height - widthAndHeight) / 2;//图形距离上侧的距离

    
    //先画出边框外围的固定的框线
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, leftSmallX, centerPoint.y);
    CGContextAddLineToPoint(context, centerPoint.x , topSmallY);
    CGContextAddLineToPoint(context, self.width - leftSmallX, centerPoint.y);
    CGContextAddLineToPoint(context, centerPoint.x, self.height - topSmallY);
    CGContextAddLineToPoint(context, leftSmallX, centerPoint.y);
    CGContextAddLineToPoint(context, self.width - leftSmallX, centerPoint.y);
    CGContextMoveToPoint(context, leftSmallX, centerPoint.y);
    CGContextAddLineToPoint(context, centerPoint.x , topSmallY);
    CGContextAddLineToPoint(context, centerPoint.x,  self.height - topSmallY);
    CGContextClosePath(context);
    //设置边框颜色
    [[UIColor whiteColor] setFill];
    [COLOR_RGBA(246, 246, 246, 1) setStroke];
    CGContextDrawPath(context, kCGPathFillStroke);
    
    //设置圆点view 坐标
    UIView * leftView = [self viewWithTag:VIEW_TYPE_LEFT];
    [leftView setCenter:CGPointMake(leftSmallX - leftView.width, centerPoint.y)];
    UIView * topView = [self viewWithTag:VIEW_TYPE_TOP];
    [topView setCenter:CGPointMake(centerPoint.x , topSmallY - topView.height)];
    UIView * rightView = [self viewWithTag:VIEW_TYPE_RIGHT];
    [rightView setCenter: CGPointMake(leftSmallX + widthAndHeight + rightView.width, centerPoint.y)];
    UIView * bottomView = [self viewWithTag:VIEW_TYPE_BOTTOM];
    [bottomView setCenter:CGPointMake(centerPoint.x , topSmallY + widthAndHeight + bottomView.height)];

    
    //开始画内部有值的边框 坐标为左0 上1 右2 下3
    if (self.dataArray.count > 0) {
        
        CGFloat leftX = centerPoint.x - [self returntDistanceWithWidthOrHeight:triangleWidth withControlNum:[self.dataArray[0] integerValue] withMaxControlNum:5] ;
        CGFloat topY = centerPoint.y - [self returntDistanceWithWidthOrHeight:triangleHeight withControlNum:[self.dataArray[1] integerValue] withMaxControlNum:5];
        CGFloat rightX = centerPoint.x + [self returntDistanceWithWidthOrHeight:triangleWidth withControlNum:[self.dataArray[2] integerValue] withMaxControlNum:5];
        CGFloat bottomY = centerPoint.y + [self returntDistanceWithWidthOrHeight:triangleHeight withControlNum:[self.dataArray[3] integerValue] withMaxControlNum:4];
        
        //设置中心分数label
        CGFloat scoreLableH = 40 * __MAIN_RATIO_375;
        NSDictionary * coreAttributes = @{NSFontAttributeName:self.scoreLabel.font};
        CGSize scoreLabelSize = [self.totalPointsStr boundingRectWithSize:CGSizeMake(__MAIN_WIDTH / 3, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:coreAttributes context:nil].size;
        [self.scoreLabel setFrame:CGRectMake(centerPoint.x - scoreLabelSize.width / 2, centerPoint.y - scoreLableH / 2, scoreLabelSize.width + 20, scoreLableH)];
        
        NSString * scoreText = [NSString stringWithFormat:@"%@分",self.totalPointsStr];
        NSMutableAttributedString * nameAttributeStr = [[NSMutableAttributedString alloc]initWithString:scoreText];
        NSRange nameRange = NSMakeRange([scoreText length] - 1, 1);
        [nameAttributeStr addAttribute:NSFontAttributeName value:FONT_DEFAULT_Light(12) range:nameRange];
        self.scoreLabel.attributedText = nameAttributeStr;
        
        //设置label 的 坐标
        CGFloat leftAndRightlabelW = 60;
        CGFloat centerLabelW = 100;
        CGFloat labelH = 20;
        CGFloat lableAndCornerY = 15 * __MAIN_RATIO_375;
        NSArray * titleArr = @[@"速度",@"急加速",@"急减速",@"疲劳驾驶"];
        UILabel * leftLabel = (UILabel *)[self viewWithTag:LABEL_TYPE_LEFT ];
        leftLabel.textAlignment = NSTextAlignmentRight;
        [leftLabel setFrame:CGRectMake(leftSmallX - leftAndRightlabelW + 5 * __MAIN_RATIO_375, centerPoint.y - lableAndCornerY - labelH, leftAndRightlabelW, labelH)];
        leftLabel.text = titleArr[0];
        UILabel * topLabel = (UILabel *)[self viewWithTag:LABEL_TYPE_TOP];
        [topLabel setFrame:CGRectMake(centerPoint.x - centerLabelW / 2, topSmallY - lableAndCornerY - labelH, centerLabelW, labelH)];
        topLabel.text = titleArr[1];
        UILabel * rightLabel = (UILabel *)[self viewWithTag:LABEL_TYPE_RIGHT];
        rightLabel.textAlignment = NSTextAlignmentLeft;
        [rightLabel setFrame:CGRectMake(leftSmallX + widthAndHeight - 5 * __MAIN_RATIO_375, centerPoint.y - lableAndCornerY - labelH, leftAndRightlabelW, labelH)];
        rightLabel.text = titleArr[2];
        UILabel * bottomLabel = (UILabel *)[self viewWithTag:LABEL_TYPE_BOTTOM];
        [bottomLabel setFrame:CGRectMake(centerPoint.x - centerLabelW / 2, topSmallY + widthAndHeight + lableAndCornerY , centerLabelW, labelH)];
        bottomLabel.text = titleArr[3];
        
        NSDictionary * topTimeAttributes = @{NSFontAttributeName:topLabel.font};
        CGSize topTimeLabelSize = [topLabel.text boundingRectWithSize:CGSizeMake(__MAIN_WIDTH - 22 * __MAIN_RATIO_375, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:topTimeAttributes context:nil].size;
        CGFloat timesLabelH = 19;
        CGFloat timesLabelY = 7;
        [self.topTimesLabel setFrame: CGRectMake(centerPoint.x - topTimeLabelSize.width / 2, topLabel.top - timesLabelY - timesLabelH , topTimeLabelSize.width, timesLabelH)];
        [self.rightTimesLabel setFrame:CGRectMake(rightLabel.left, rightLabel.top - timesLabelY - timesLabelH, topTimeLabelSize.width, timesLabelH)];
        
        CGContextRef contextOne = UIGraphicsGetCurrentContext();
        CGContextRef contextTwo = UIGraphicsGetCurrentContext();
        CGContextRef contextThree = UIGraphicsGetCurrentContext();
        CGContextRef contextFour = UIGraphicsGetCurrentContext();
        [self creatTriangleWithContext:contextOne withFillColor:COLOR_HEXSTRING(@"#1D9FE6") withPointOne:CGPointMake(leftX, centerPoint.y) withPointTwo:CGPointMake(centerPoint.x, topY) withPointThree:centerPoint];
        [self creatTriangleWithContext:contextTwo withFillColor:COLOR_HEXSTRING(@"#82CAF1") withPointOne:CGPointMake(centerPoint.x, topY) withPointTwo:CGPointMake(rightX, centerPoint.y) withPointThree:centerPoint];
        [self creatTriangleWithContext:contextThree withFillColor:COLOR_HEXSTRING(@"#57B7ED") withPointOne:CGPointMake(rightX, centerPoint.y) withPointTwo:CGPointMake(centerPoint.x, bottomY) withPointThree:centerPoint];
        [self creatTriangleWithContext:contextFour withFillColor:COLOR_HEXSTRING(@"#30A6E7") withPointOne:CGPointMake(centerPoint.x, bottomY) withPointTwo:CGPointMake(leftX, centerPoint.y) withPointThree:centerPoint];
    }
    
    
}
//绘画每个小三角形 输入三角形context  颜色 三个点的坐标
- (void)creatTriangleWithContext:(CGContextRef)context withFillColor:(UIColor *)color withPointOne:(CGPoint)pointOne withPointTwo:(CGPoint)pointTwo withPointThree:(CGPoint)pointThree
{
    CGContextBeginPath(context);
    CGContextMoveToPoint(context, pointOne.x, pointOne.y);
    CGContextAddLineToPoint(context, pointTwo.x, pointTwo.y);
    CGContextAddLineToPoint(context, pointThree.x, pointThree.y);
    CGContextAddLineToPoint(context, pointOne.x, pointOne.y);
    CGContextClosePath(context);
    [color setFill];
    [[UIColor clearColor]setStroke];
    CGContextDrawPath(context, kCGPathFillStroke);
}
//返回每个三角形距离中心点的距离 每个三角形整个宽度最大值triangleWidthOrHeight 每个三角形整个高度最大值triangleHeight 控制该点三角形的减少的基本数值
- (CGFloat)returntDistanceWithWidthOrHeight:(CGFloat)triangleWidthOrHeight withControlNum:(NSInteger)controlNum withMaxControlNum:(NSInteger)maxControlNum
{
    CGFloat minTriangleWidth = triangleWidthOrHeight / 2;//每个小三角形宽度的最小值
    CGFloat distance = (controlNum < maxControlNum ? (triangleWidthOrHeight - minTriangleWidth / maxControlNum * controlNum) : minTriangleWidth);
    return distance;
}
- (void)setFuncArray:(NSArray *)funcArray
{
    self.dataArray = [NSArray arrayWithArray:funcArray];
    [self setNeedsDisplay];
}
- (void)setTopTimeCountInt:(NSInteger)topTimeCountInt
{
    self.topTimesLabel.text = [NSString stringWithFormat:@"%zd 次",topTimeCountInt];
}
- (void)setRightTimesCountInt:(NSInteger)rightTimesCountInt
{
    self.rightTimesLabel.text = [NSString stringWithFormat:@"%zd 次",rightTimesCountInt];
}

//创建 n次上升 下降（图片） 的view
//- (UIView * )timesViewWithSize:(CGSize)viewSize
//{
//    UIView * backView = [Factory viewWithFrame:CGRectZero bgColor:COLOR_HEXSTRING(@"#F3F3F3") onView:self];
//    backView.size = viewSize;
//    UILabel * timeLabel = [Factory labelWithFrame:CGRectMake(5, 0, 30, viewSize.height) font:FONT_DEFAULT_Light(12) text:@"2次" textColor:[Theme wordColor] onView:backView textAlignment:NSTextAlignmentLeft];
//    timeLabel.tag = TIMES_LABEL_TAG;
//    UIImageView * imageV = [Factory imageViewWithCenter:CGPointMake(backView.width - 5 - 5, backView.height / 2) image:nil onView:backView];
//    imageV.backgroundColor = [UIColor redColor];
//    imageV.tag = TIMES_IMAGE_V_TAG;
//    return backView;
//}
@end
